package com.wiim.ui;

import java.util.ArrayList;
import java.util.Arrays;

import com.wiim.adapter.DanhSachAdapter;
import com.wiim.interfaceapp.IServiceCommandResult;
import com.wiim.handler.SHOPCmdHandler;
import com.wiim.nhapnhap.AppGlobal;
import com.wiim.model.DoithuongModel;
import com.wiim.model.ShopData;
import com.wiim.nhapnhap.EgoodApp;
import com.wiim.nhapnhap.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class DanhSachFragment extends Fragment implements IServiceCommandResult {
	private ListView lvDanhSach;
	private DanhSachAdapter dataAdapter;
	public static ArrayList<ShopData> datalist;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.danhsach_list, container, false);
		initFeld(view);
		// initData();
		return view;
	}

	private void initFeld(View view) {
		lvDanhSach = (ListView) view.findViewById(R.id.lvDanhSach);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void bindData(ShopData[] shoplst) {
		datalist = new ArrayList<ShopData>(Arrays.asList(shoplst));

		dataAdapter = new DanhSachAdapter(getActivity(), datalist);
		lvDanhSach.setAdapter(dataAdapter);

	}

	private void initData() {
		Bundle cmdData = new Bundle();
		cmdData.putString(EgoodApp.REST_OBJECT_PUT, "*");
		AppGlobal.getInstance(DanhSachFragment.this).startServiceCmd(SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID, new SHOPCmdHandler(), cmdData, true);
	}

	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		Bundle bResultData = (Bundle) resultData;

		switch (commandCode) {
		case SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID: {
			ShopData[] shoplst = (ShopData[]) bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);
			bindData(shoplst);
		}
			break;

		default:
			break;
		}
	}
}
